﻿using MengLin.OOP.DesignPattern.WMS_LC4.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.WMS_LC4.Implements.Scheduler
{
    /// <summary>
    /// 警报时钟
    /// 版本3调度器做了1件事
    /// 1.进行调度
    /// </summary>
    public class AlarmClock : AbstractScheduler
    {
        /// <summary>
        /// 容器
        /// </summary>
        private static Dictionary<int, IAlarmListener> _container;
        private static Dictionary<int, AbstractTemperatureSensor> _container2;
        private static Dictionary<int, IBarometricPressureSensor> _container3;

        /// <summary>
        /// 注册警报侦听器
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="alarmListener"></param>
        public static void WakeEvery(int interval, IAlarmListener alarmListener)
        {
            if (_container == null)
            {
                _container = new Dictionary<int, IAlarmListener>();
                _container.Add(interval, alarmListener);
            }
            else
            {
                _container.Add(interval, alarmListener);
            }
        }

        /// <summary>
        /// 注册温度传感器
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="tempSensor"></param>
        public static void WakeEvery2(int interval, AbstractTemperatureSensor tempSensor)
        {
            if (_container2 == null)
            {
                _container2 = new Dictionary<int, AbstractTemperatureSensor>();
                _container2.Add(interval, tempSensor);
            }
            else
            {
                _container2.Add(interval, tempSensor);
            }
        }

        /// <summary>
        /// 注册气压传感器
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="tempSensor"></param>
        public static void WakeEvery3(int interval, IBarometricPressureSensor barometricPressureSensor)
        {
            if (_container3 == null)
            {
                _container3 = new Dictionary<int, IBarometricPressureSensor>();
                _container3.Add(interval, barometricPressureSensor);
            }
            else
            {
                _container3.Add(interval, barometricPressureSensor);
            }
        }

        /// <summary>
        /// 闹钟不停的走
        /// </summary>
        public override void Tic()
        {
            while (true)
            {
                foreach (var item in _container)
                {
                    //Wakeup
                    item.Value.Wakeup();
                }
                Thread.Sleep(2000);
            }
        }
    }
}
